<?php

class Gm_getServerListByGame
{
    function getServerListByGame($game)
    {

        $readByConf = MooConfig::get('main.readByConf');

        $gameLangConf = MooConfig::get('game_lang.zh_en');
        if ($gameLangConf[$game]) {
            $game = $gameLangConf[$game];
        }

        // 走配置
        if ($readByConf && $readByConf == 1) {
            $resArr = MooConfig::get('server.' . $game);
            $resData = array();
            if ($resArr) {
                foreach ($resArr as $key => $val) {
                    $arr['id'] = $key;
                    $arr['platform'] = $val['platform'];
                    $arr['serverId'] = $key + 1;
                    $arr['server'] = $val['server'];
                    $arr['url'] = $val['ip'] . ":" . $val['script_port'];
                    $resData[$arr['server']] = $arr;
                }
            }
        } else {
            $getServersUrl = MooConfig::get('main.url.fetch_server');
            $url = $getServersUrl . "?game=" . $game;

            $res = MooUtil::curl_send($url);

            $resArr = MooJson::decode($res, true);

            $resData = array();
            if ($resArr) {
                foreach ($resArr as $key => $val) {
                    $arr['id'] = $key;
                    $arr['platform'] = $val['platform'];
                    $arr['serverId'] = $val['server'];
                    $arr['server'] = $val['platform'] . "-" . $val['server'];
                    $arr['url'] = $val['ip'] . ":" . $val['script_port'];
                    $resData[$arr['server']] = $arr;
                }
            }

        }

        MooArray::tdSort($resData, "platform", SORT_ASC, "serverId", SORT_ASC);

        return $resData;
    }

}